﻿var ywsfc_jgid;
$(function () {
    //======================
    //======设置查询条件========
    //======================
    //判断查询范围，如果查询范围不是全部不允许更改机构
    /*if ($("#ywsfc_cxfw").val() != 0) {
     ywsfc_jgid = $("#ywsfc_jg").val();
     $("#ywsfc_gsId").val($("#ywsfc_jgmc").val());
     $("#ywsfc_gsId").attr("readonly", true);

     //$("#ywsfc_gsId").jygsjgtree();
     $("#ywsfc_gsId").ssgsjgcombotree();
     } else {
     $("#ywsfc_gsId").jygsjgcombotree();
     // $("#ywsfc_gsId").gsjgtree(1);
     }*/
   // $("#ywsfc_gsId").jygsjgcombotree();    jygsjgwqxcombotree
    $("#ywsfc_gsId").jygsjgwqxcombotree();
    //物资大类加载
    $("#ywsfc_toolbar [name='ywsfc_wzdl']").wzdlCombobox();
    //加载仓库
    $("#ywsfc_ck").combobox({
        url:$.webPath() + '/organ/ck/sskcCkmcList',
        valueField:'id',
        textField:'mc',
        panelHeight:'200',
        editable:true
    });

    $("#ywsfc_gsId").combotree({
        onChange:function (newValue, oldValue) {
            if (newValue != oldValue) {
                $("#ywsfc_ck").combobox('reload', $.webPath() + '/organ/ck/sskcCkmcListForSearch?gsId=' + newValue);
                $("#ywsfc_ck").combobox("clear");
            }
        }
    })

    //======================导出==========================
    $("#ywsfc_toolbar [name='ywsfc_dc']").click(function () {
        /* var gsid;
         if ($("#ywsfc_cxfw").val() != 0) {
         // gsid = ywsfc_jgid;
         gsid = $("#ywsfc_gsId").combotree("getValue");
         } else {
         gsid = $("#ywsfc_gsId").combotree("getValue");
         }*/
        // gsid = $("#ywsfc_gsId").combotree("getValue");
        window.open($.webPath() + "/tjbb/ywsfcbb/exportywsfc?begindate=" + $("#ywsfc_start").datebox("getValue") +
            "&enddt=" + $("#ywsfc_end").datebox("getValue") +
            //"&gsid=" +gsid+
            "&gsid=" + $("#ywsfc_toolbar [name='ywsfc_gsId']").val() +
            "&wz_type=" + $("#ywsfc_toolbar [name='ywsfc_wzlb']").val() +
            "&select_type=" + $("#ywsfc_toolbar [name='ywsfc_lx']").val() +
            "&ck_id=" + $("#ywsfc_toolbar [name='ywsfc_ck']").val() +
            "&wzdl=" + $("#ywsfc_toolbar [name='ywsfc_wzdl']").val());
    });

    //==============================搜索按钮
    $("#ywsfc_toolbar [name='searchButton']").click(function () {
        ywsfc_search();
    });
    function ywsfc_search() {
        /* var gsid;
         if ($("#ywsfc_cxfw").val() != 0) {
         //     gsid = ywsfc_jgid;
         gsid = $("#ywsfc_gsId").combotree("getValue");
         } else {
         gsid = $("#ywsfc_gsId").combotree("getValue");
         }*/
        //gsid = $("#ywsfc_gsId").combotree("getValue");
        $(grid).datagrid({
            queryParams:{
                ck_id:$("#ywsfc_toolbar [name='ywsfc_ck']").val(),
                gsid:$("#ywsfc_toolbar [name='ywsfc_gsId']").val(),
                // gsid: gsid,
                //gsid: $("#ywsfc_gsId").combotree("getValue"),
                wzdl:$("#ywsfc_toolbar [name='ywsfc_wzdl']").val(),
                begindate:$("#ywsfc_toolbar [name='ywsfc_start']").val(),
                enddt:$("#ywsfc_toolbar [name='ywsfc_end']").val(),
                wz_type:$("#ywsfc_toolbar [name='ywsfc_wzlb']").val(),
                select_type:$("#ywsfc_toolbar [name='ywsfc_lx']").val()

            }});
    }

    ;

//======================
//========统计列表========
//======================
    var columns = [
        [
            {field:'gs_name', title:'公司名称', rowspan:2, width:100, align:'center'},
            {field:'pz', title:'品种', rowspan:2, width:100, align:'left'},
            // {field: 'code', title: '物资编码', rowspan: 2, width: 100,align:'center'},
            /*    {field: 'oldcode', title: '老物资编码', rowspan: 2, width: 100,align:'center'},*/
            {field:'qc', title:'期初', rowspan:2, width:100, align:'right', formatter:function (v, r, i) {
                var color = "";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return '<span style="color:' + color + '" >' + formatSl(v, ",", ".") + '</span>';
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {title:'本期入库', colspan:3, width:100},
            {title:'本期出库', colspan:4, width:100},
            {field:'qm', title:'期末', rowspan:2, width:100, align:'right', formatter:function (v, r, i) {
                var color = "";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return '<span style="color:' + color + '" >' + formatSl(v, ",", ".") + '</span>';
                } else {
                    return formatSl(v, ",", ".");
                }
            }}
        ],
        [
            {field:'rk', title:'采购', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {                                          //gsid,select_type,wzbh,begindate,enddt
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqrkcg' gsid='{1}' pz='{2}'  >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {field:'zr', title:'拨入', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqrkzr' gsid='{1}' pz='{2}' >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {field:'rktz', title:'调整', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqrkrktz' gsid='{1}' pz='{2}'  >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},

            {field:'ck', title:'销售', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqckck' gsid='{1}' pz='{2}' >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {field:'zc', title:'拨出', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqckzc' gsid='{1}' pz='{2}'>{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {field:'cktz', title:'调整', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqckcktz' gsid='{1}' pz='{2}' >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }},
            {field:'nbly', title:'内部领用', width:100, align:'right', formatter:function (v, r, i) {
                var color = "#0000ff;";
                if (v && r.gs_name != '合计') {
                    if (v < 0) color = "red;";    //
                    return "<a href='javascript:void(0)' style='color:{0}' class='bqcknbly' gsid='{1}' pz='{2}' >{3}</a>".stringFormat(color, r.gs_id, r.pz, formatSl(v, ",", "."));
                } else {
                    return formatSl(v, ",", ".");
                }
            }}
        ]
    ];
    /* var gsid;
     if ($("#ywsfc_cxfw").val() != 0) {
     //gsid = ywsfc_jgid;
     gsid = $("#ywsfc_gsId").combotree("getValue");
     } else {
     gsid = $("#ywsfc_gsId").combotree("getValue");
     }*/
    // gsid = $("#ywsfc_gsId").combotree("getValue");
    var grid = $('#ywsfc_grid').datagrid({
        url:$.webPath() + '/tjbb/ywsfcbb/ywsfcList',
        toolbar:"#ywsfc_toolbar",
        striped:true,
        pagination:true,
        pageSize:100,
        rownumbers:true,
        singleSelect:true,
        collapsible:true,
        fit:true,
        pageNumber:1,
        fitColumns:true,
        showFooter:true,
        /* queryParams: {
         ck_id:$("#ywsfc_toolbar [name='ywsfc_ck']").val(),
         gsid: $("#ywsfc_toolbar [name='ywsfc_gsId']").val(),
         //gsid: gsid,
         //  gsid: $("#ywsfc_gsId").combotree("getValue"),
         wzdl: $("#ywsfc_toolbar [name='ywsfc_wzdl']").val(),
         begindate: $("#ywsfc_toolbar [name='ywsfc_start']").val(),
         enddt: $("#ywsfc_toolbar [name='ywsfc_end']").val(),
         wz_type: $("#ywsfc_toolbar [name='ywsfc_wzlb']").val(),
         select_type: $("#ywsfc_toolbar [name='ywsfc_lx']").val()
         },*/
        onLoadSuccess:function () {
            $(".bqrkcg").unbind("click").bind("click", function () {                                       //wzdl wz_type
                // alert($(this).attr('pz'));
                showDetail('本期入库采购', $.webPath() + "/tjbb/ywsfcbb/bqrkcg", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqrkzr").unbind("click").bind("click", function () {
                showDetail('本期入库拨入', $.webPath() + "/tjbb/ywsfcbb/bqrkzr", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqrkrktz").unbind("click").bind("click", function () {
                showDetail('本期入库调整', $.webPath() + "/tjbb/ywsfcbb/bqrkrktz", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqckck").unbind("click").bind("click", function () {
                showDetail('本期出库销售', $.webPath() + "/tjbb/ywsfcbb/bqckck", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqckzc").unbind("click").bind("click", function () {
                showDetail('本期出库拨出', $.webPath() + "/tjbb/ywsfcbb/bqckzc", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqckcktz").unbind("click").bind("click", function () {
                showDetail('本期出库调整', $.webPath() + "/tjbb/ywsfcbb/bqckcktz", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".bqcknbly").unbind("click").bind("click", function () {
                showDetail('本期出库内部领用', $.webPath() + "/tjbb/ywsfcbb/bqcknbly", $(this).attr("gsid"), $(this).attr("pz"), $(this).attr("ck_id"), $(this).attr("wz_type"), $(this).attr("select_type"), $(this).attr("begindate"), $(this).attr("enddt"));
            });
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        columns:columns
    });
//===========================================
//==============此页面用到的函数===================
//===========================================

    var showDetail = function (title, url, gsid, pz, ck_id, wz_type, select_type, begindate, enddt) {
        var win = $.window({
            title:title,
            width:1050,
            height:470,
            cache:false, // wzdl: $("#ywsfc_toolbar [name='ywsfc_wzdl']").val(),     wz_type
            iconCls:'icon-tip',
            url:encodeURI(encodeURI(url + "?gsid=" + gsid + "&pz=" + pz + "&ck_id=" + $("#ywsfc_toolbar [name='ywsfc_ck']").val() + "&wz_type=" + $("#ywsfc_toolbar [name='ywsfc_wzlb']").val() + "&select_type=" + $("#ywsfc_toolbar input[name='ywsfc_lx']").val() + "&begindate=" + $("#ywsfc_start").datebox("getValue") + "&enddt=" + $("#ywsfc_end").datebox("getValue"))),
            modal:true,
            buttons:[]
        });
    }
});
